package com.yazi.mxz.server.controller;


import javax.annotation.Resource;

import mo.framework.smh.common.exception.MyException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.annotation.JsonView;
import com.yazi.mxz.common.entity.beans.Member;
import com.yazi.mxz.common.entity.beans.Shopping;
import com.yazi.mxz.common.exception.HcdException;
import com.yazi.mxz.common.service.ShoppingService;
import com.yazi.mxz.server.response.IntegerResponse;
import com.yazi.mxz.server.response.ShoppingAddResponse;
import com.yazi.mxz.server.response.StatusResponse;
import com.yazi.mxz.server.response.view.BaseView;

@Controller("shoppingController")
@RequestMapping("/shopping")
public class ShoppingController extends BaseController {
	private static Logger log = LoggerFactory.getLogger(ShoppingController.class);
	
	
	@Resource
	ShoppingService shoppingService;
	
	@RequestMapping("/addDo")
	@ResponseBody
	@JsonView(BaseView.class)
	public ShoppingAddResponse addDo(@RequestBody Shopping shopping) {
		log.debug("客户端 -> 购物车添加商品");
		ShoppingAddResponse res = new ShoppingAddResponse();
		try {
			shopping = shoppingService.add(getLoginMember(), shopping);
			res.setSuccessData(shopping);
			res.setOkMessage("成功加入购物车!");
		} catch (HcdException e) {
			log.debug("购物车添加商品失败:{}", e.getMessage());
			res.setCode(e.getCode());
			res.setFailData((Shopping)e.getData());
			res.setFailMessage(e.getMessage());
		} catch (Exception e) {
			log.error("购物车添加商品异常:", e);
			res.setErrorMessage("操作失败,请联系管理员");
		}
		return res;
	}
	
	@RequestMapping("/delDo")
	@ResponseBody
	public StatusResponse delDo(@RequestBody Long[] shoppingIds) {
		Member loginMember = getLoginMember();
		Long memberId = loginMember.getMemberId();
		log.debug("客户端 -> 删除购物车商品：memberId={},ids={}", memberId, shoppingIds);
		StatusResponse res = new StatusResponse();
		try {
			shoppingService.delete(memberId, shoppingIds);
			res.setOkMessage("删除成功!");
		} catch (Exception e) {
			log.error("删除购物车商品异常:", e);
			res.setErrorMessage(e.getMessage());
		}
		
		return res;
	}
	
	@RequestMapping("/updateDo")
	@ResponseBody
	public StatusResponse updateDo(@RequestBody Shopping shopping) {
		Member loginMember = getLoginMember();
		Long memberId = loginMember.getMemberId();
		log.debug("客户端 -> 更新购物车商品购买量：memberId={},shoppingId={},buyCount={}", 
				new Object[]{memberId, shopping.getShoppingId(), shopping.getBuyCount()});
		StatusResponse res = new StatusResponse();
		try {
			shoppingService.update(loginMember, shopping);
			res.setOkMessage("操作成功!");
		} catch (HcdException e) {
			log.debug("更新购物车商品购买量失败[{}]:{}", e.getCode(), e.getMessage());
			res.setCode(e.getCode());
			res.setFailMessage(e.getMessage());
		} catch (MyException e) {
			log.debug("更新购物车商品购买量失败[{}]:{}", e.getClass().getSimpleName(), e.getMessage());
			res.setFailMessage("操作失败");
		} catch (Exception e) {
			log.error("更新购物车商品购买量异常:", e);
			res.setErrorMessage("操作失败,请联系管理员");
		}
		
		return res;
	}
	
	@RequestMapping("/count")
	@ResponseBody
	public IntegerResponse count() {
		log.debug("客户端请求 -> 获取购物车商品数");
		IntegerResponse res = new IntegerResponse();
		try{
			int count = shoppingService.getCount(getLoginMember().getMemberId(), null);
			res.setSuccessData(count);
			res.setOk();
		} catch (Exception e) {
			log.error("客户端请求 -> 获取购物车商品数时异常:", e);
			res.setErrorMessage(e.getMessage());
		}
		return res;
	}
}
